/**
 * AUTO_COPYRIGHT_SUB_TAG
 */
package com.rim.samples.device.push;

import java.util.Timer;
import java.util.TimerTask;

import log.Logger;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.TransitionContext;
import net.rim.device.api.ui.UiApplication;
import runnables.VersionRunnable;
import screens.ConfigurationScreen;
import screens.SplashScreen;

import com.rim.samples.device.push.lib.PushLibFactory;
import com.rim.samples.device.push.ui.MessageViewScreen;
import com.rim.samples.device.push.ui.RimLogScreen;
import com.rim.samples.device.push.ui.WelcomeScreen;

/**
 * Main entry point for the push application. Creates application instance and pushes the welcome screen.
 */
public class PushMain {

	private static WelcomeScreen welcomeScreen;
	private static MessageViewScreen messageScreen;
	private static RimLogScreen logScreen;
	//private static ConfigScreen configScreen;
	//private static MessageHistoryScreen historyScreen;

	public static void main( String[] args ) {

		// create application instance
		UiApplication application = PushLibFactory.getUiApplication();

		// create all the screens in application
		//		welcomeScreen = new WelcomeScreen();
		//		messageScreen = new MessageViewScreen();
		//		logScreen = new RimLogScreen();
		//		configScreen = new ConfigScreen();
		//		historyScreen = new MessageHistoryScreen();

		// push the welcome screen
		//application.pushScreen( welcomeScreen );

		SplashScreen splash = new SplashScreen(TransitionContext.TRANSITION_FADE);
		application.pushScreen(new ConfigurationScreen());
		application.pushScreen(splash);
		kill(splash);
		System.out.println("done creating application");
		checkVersion();

		// notify push controller
		// etapa 3: uncomment for etapa 3
		PushController.appStarted();

		//TODO etapa 3: check these 2
		// and update labels
		//PushController.updateCountLabels();

		// process events
		application.enterEventDispatcher();
	}

	public static WelcomeScreen getWelcomeScreen() {
		return welcomeScreen;
	}

	public static MessageViewScreen getMessageViewScreen() {
		return messageScreen;
	}

	public static RimLogScreen getLogScreen() {
		return logScreen;
	}

//	public static ConfigScreen getConfigScreen() {
//		return configScreen;
//	}
//
//	public static MessageHistoryScreen getHistoryScreen() {
//		return historyScreen;
//	}

	public static void kill(final Screen screen) {
		Timer a = new Timer();
		TimerTask b;
		b = new TimerTask(){
			public void run() {
				try
				{
					UiApplication.getUiApplication().invokeAndWait(closeScreen);
				}
				catch(NullPointerException npe)
				{
					System.out.println(npe);
				}
			}
		};
		a.schedule(b,5000);
	}

	static Runnable closeScreen=new Runnable(){
		public void run() {
			closeScr();
		}
	};
	private static void closeScr() {                                                      
		UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
	}

	public static void checkVersion() {
		Timer a = new Timer();
		TimerTask b;
		b = new TimerTask(){
			public void run() {
				try
				{
					UiApplication.getUiApplication().invokeAndWait(new VersionRunnable());
					Logger.log("checking for version, timer task");
				}
				catch(NullPointerException npe)
				{
					System.out.println(npe);
				}
			}
		};
		//  once per day
		a.scheduleAtFixedRate(b, 3600000, 86400000);
		//a.scheduleAtFixedRate(b, 360, 1000);
	}

}
